package com.software.questioncenter.service;

import com.software.questioncenter.entity.Question;
import com.software.questioncenter.exception.BizException;
import com.software.questioncenter.repository.QuestionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Author: wjx
 * Description: 题目Question的服务层实现类
 */
@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionRepository questionRepository;

    //新增题目
    public String createQuestion(Question question) {
        //查询题目是否在题库中存在
        Optional<Question> optional = questionRepository.findById(question.getId());
        //如果存在，即q不为null，则报异常
        if (optional.isPresent()){
            throw new BizException("该题目已经在题库中存在");
        }
        //如果不存在，则插入题目，并返回信息
        questionRepository.save(question);
        return "新增成功";
    }

    //删除
    public String deleteQuestion(Integer questionId){
        Optional<Question> optional = questionRepository.findById(questionId);
        if (optional.isPresent()){
            questionRepository.deleteById(questionId);
            return "删除成功";
        }
        throw new BizException("未查询到需要删除的题目，请核对题目id是否正确");
    }

    //更改
    public String updateQuestion(Question question){
        Optional<Question> optional = questionRepository.findById(question.getId());
        if (optional.isPresent()){
            questionRepository.save(question);
            return "修改成功";
        }
        throw new BizException("未查询到需要修改的题目，请核对题目id是否正确");
    }

    //查询所有题目
    public Page findAll(Pageable pageable){
        Page page = questionRepository.findAll(pageable);
        return page;
    }

    //查看详情
    public Question findById(Integer questionId){
        Optional<Question> optional = questionRepository.findById(questionId);
        if (!optional.isPresent()){
            throw new BizException("未查询到需要查看的题目，请核对题目id是否正确");
        }
        return optional.get();
    }

    //根据难度查询题目
    @Override
    public Page findByDegree(String degree, Pageable pageable) {
        Specification<Question> specification = new Specification<Question>() {
            @Override
            public Predicate toPredicate(Root<Question> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (!StringUtils.isEmpty(degree)){
                    predicates.add(criteriaBuilder.equal(root.get("degree"), degree));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page page = questionRepository.findAll(specification,pageable);
        return page;
    }

    //根据题目模糊查询
    @Override
    public Page findByName(String title, Pageable pageable) {
        Specification<Question> specification = new Specification<Question>() {
            @Override
            public Predicate toPredicate(Root<Question> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (!StringUtils.isEmpty(title)){
                    predicates.add(criteriaBuilder.like(root.get("title"), "%" + title + "%"));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page page = questionRepository.findAll(specification,pageable);
        return page;
    }
    //查找题目数量的方法
    @Override
    public Integer findQuestionNums() {
        List<Question> all = questionRepository.findAll();
        if(all!=null){
            return all.size();
        }else{
            return 0;
        }
    }

}
